import Stripe from 'stripe';
import dotenv from 'dotenv';

// Load environment variables first
dotenv.config();

if (!process.env.STRIPE_SECRET_KEY) {
  console.error('❌ STRIPE_SECRET_KEY is not defined in environment variables');
  console.error('📝 Please create a .env file in apps/backend/ with your Stripe keys');
  console.error('💡 Example: STRIPE_SECRET_KEY=sk_test_your_key_here');
  throw new Error('STRIPE_SECRET_KEY is not defined in environment variables');
}

// Initialize Stripe with your secret key
export const stripe = new Stripe(process.env.STRIPE_SECRET_KEY, {
  apiVersion: '2023-10-16',
  typescript: true,
});

// Stripe configuration
export const stripeConfig = {
  secretKey: process.env.STRIPE_SECRET_KEY,
  publishableKey: process.env.STRIPE_PUBLISHABLE_KEY,
  webhookSecret: process.env.STRIPE_WEBHOOK_SECRET,
  
  // Default currency
  defaultCurrency: 'usd' as const,
  
  // Supported currencies
  supportedCurrencies: ['usd', 'eur', 'gbp', 'cny', 'jpy'] as const,
  
  // Payment method types - support card, WeChat Pay, Crypto, AliPay, and Bank Transfer
  paymentMethodTypes: ['card', 'wechat_pay', 'customer_balance', 'alipay', 'us_bank_account'] as const,
  
  // Payment method options for better control
  paymentMethodOptions: {
    card: {
      // Support both debit and credit cards
      request_three_d_secure: 'automatic' as const,
    },
    wechat_pay: {
      // WeChat Pay specific options
      client: 'web' as const,
    },
    customer_balance: {
      // For stablecoins and crypto through customer balance
      funding_type: 'bank_transfer' as const,
    },
  },
};

export default stripe;

